const TOKEN_KEY = 'token';

const isLogin = () => {
  return !!localStorage.getItem(TOKEN_KEY);
};

const getToken = () => {
  return localStorage.getItem(TOKEN_KEY);
};

const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token);
};

const clearToken = () => {
  localStorage.removeItem(TOKEN_KEY);
};function containsMenuCode(menuList, targetCode) {
  if (!menuList || menuList.length === 0) {
      return false;
  }

  let menuCodes = [];

  function traverseMenu(menu) {
      if (menu.menuCode) {
          menuCodes.push(menu.menuCode);
      }
      if (menu.children && menu.children.length > 0) {
          menu.children.forEach(child => traverseMenu(child));
      }
  }

  menuList.forEach(menu => traverseMenu(menu));
  return menuCodes.includes(targetCode);
}

const isAuth = (menuCode) => {
  let userInfo = JSON.parse(localStorage.getItem('userInfo')) || {};
  const menuList = userInfo.menuList || [];
  if(window.location.host === 'fuwu-test.habitat.cn'){
    return true
  }
  return containsMenuCode(menuList, menuCode);
};

export { isLogin, getToken, setToken, clearToken,isAuth };
